package translation

import csv.CsvParser;
import csvLoader.CsvLoaderService;
import translation.TranslationService;
import db.DataDeleter;
import dump.DumpService;
import system.*;
import xml.*;
import app.*;
import sugarbind_input.Ligand;
import sugarbind_input.Ligand_Type;
import sugarbind_input.Structure;




class TranslationController {


	String picFormat = "png"//"svg" //
	TranslationService translationService = new TranslationService()
	
	

	def index = {
	}

	static transactional = true

	def allowedMethods = []

	def glycoct = {
		def structures = Structure.findAll()
		int i = 0
		for(structure in structures)
		{
			String ct = translationService.glycoCT(structure.iupac)
			if(ct!=null)
			{
				i++
				structure.glycoct = ct
			} 
		}
//		"Translated $i / ${structures.size()} structures to GlycoCT"
		redirect(uri: "/translation/index")
	}
	
	
	def cfg = {		
		def structures = Structure.findAll()
		int i = 0
		for(structure in structures)
		{
			byte[] cartoon = null
			if(structure.glycoct!=null)
			{
				i++
				String filename = translationService.cartoon(structure, "cfg", picFormat)
				if(filename != null)
				{
					def file = new File(filename)
					structure.cfg = file.getBytes() 
				}
			}
		}
		redirect(uri: "/translation")
	}
	
	def uoxfcol = {
		def structures = Structure.findAll()
		int i = 0
		for(structure in structures)
		{
			byte[] cartoon = null
			if(structure.glycoct!=null)
			{
				i++
				String filename = translationService.cartoon(structure, "uoxfcol", picFormat)
				if(filename != null)
				{
					def file = new File(filename)
					structure.uoxfcol = file.getBytes()
				}
			}
		}
		redirect(uri: "/translation")
	}
	
	def text = {
		def structures = Structure.findAll()
		int i = 0
		for(structure in structures)
		{
			byte[] cartoon = null
			if(structure.glycoct!=null)
			{
				i++
				String filename = translationService.cartoon(structure, "text", picFormat)
				if(filename != null)
				{
					def file = new File(filename)
					structure.text = file.getBytes()
				}
			}
		}
		redirect(uri: "/translation")
	}
	
	def all = {
		def structures = Structure.findAll()
		int i = 0
		ArrayList<String> cartoonFormats = new ArrayList<String>() //
		cartoonFormats.addAll("cfg","uoxfcol","text")
		
		for(structure in structures)
		{
			byte[] cartoon = null
			if(structure.glycoct!=null)
			{
				i++
				for(int j=0;j<cartoonFormats.size();j++)
				{
					String filename = translationService.cartoon(structure, cartoonFormats[j], picFormat)
					
					if(filename != null)
					{
						def file = new File(filename)
						
						if(cartoonFormats[j]=="cfg")
						structure.cfg = file.getBytes()
						if(cartoonFormats[j]=="uoxfcol")
						structure.uoxfcol = file.getBytes()
						if(cartoonFormats[j]=="text")
						structure.text = file.getBytes()
					}	
				}
			}
		}
		redirect(uri: "/translation")
	}
	
	def ligand_types = {
		
		def ligands = Ligand.findAll()
				
		for(ligand in ligands)
		{
			
			if(ligand.structure.glycoct!=null)
			{
				translationService.ligand_type(ligand)
			}
		}
		redirect(uri: "/translation")
	}
	
}
